package com.shequan.weibo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ailk.dazzle.util.data.JSONUtils;
import com.shequan.common.bean.ResultDataStruct;
import com.shequan.common.bean.ViewTextConstans;
import com.shequan.user.bean.bo.UserSession;
import com.shequan.weibo.bean.pojo.Topic;
import com.shequan.weibo.bean.pojo.TopicWeibo;
import com.shequan.weibo.bean.pojo.WeiboComment;
import com.shequan.weibo.service.TopicService;
import com.shequan.weibo.service.WeiboService;

/**
 * 
 * <P>
 * Copyright 2012 shequaner. 社圈微博控制类 All Rights Reserved.
 * 
 * @author mazg
 * @version 1.0, 2012-3-14
 * @see com.shequan.weibo.controller.WeiboController
 * @since 1.0
 */
@Controller
@RequestMapping("/weibo")
public class WeiboController {
	private static final Logger log = Logger.getLogger(WeiboController.class);
	private TopicService topicService;
	private WeiboService weiboService;
	/**
	 * 查询热门话题
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-3-14 下午10:30:38
	 */
	@RequestMapping(value = "/topic/hots")
	public void queryHotTopics(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<Topic>> rds = new ResultDataStruct<List<Topic>>();
		int userId;
		try {
			UserSession userSession = (UserSession) request.getSession().getAttribute("userSession");
			if (userSession != null) {
				userId = userSession.getUserId();
				List<Topic> topicList = topicService.queryHotTopicListByUserId(userId);
				rds.setData(topicList);
				rds.setBusiSuccess(true);
			} else {
				rds.setBusiSuccess(false);
			}
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}


	/**
	 * 根据社圈ID查询社圈话题
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-3-14 下午10:30:47
	 */
	@RequestMapping(value = "/shequanTopic")
	public void shequanTopic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<Topic>> rds = new ResultDataStruct<List<Topic>>();
		String shequanIdStr;
		try {
			shequanIdStr = request.getParameter("shequanId");
			int shequanId = Integer.parseInt(shequanIdStr);
			List<Topic> topicList = topicService.queryTopicListByShequanId(shequanId);
			rds.setData(topicList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}
	
	/**
	 * 根据话题Id查询微博列表
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-3-14 下午10:30:47
	 */
	@RequestMapping(value = "/weibo")
	public void weibo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<TopicWeibo>> rds = new ResultDataStruct<List<TopicWeibo>>();
		String topicIdStr;
		try {
			topicIdStr = request.getParameter("topicId");
			int topicId = Integer.parseInt(topicIdStr);
			List<TopicWeibo> weiboList = weiboService.queryWeiboByTopicId(topicId);
			rds.setData(weiboList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}
	
	/**
	 * 发表微博
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-3-14 下午10:30:47
	 */
	@RequestMapping(value = "/publish")
	public void publish(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<TopicWeibo>> rds = new ResultDataStruct<List<TopicWeibo>>();
		String weiboStr;
		String topic;
		try {
			weiboStr = request.getParameter("weiBo");
			topic = request.getParameter("topic");
			TopicWeibo weibo = null;
//			this.weiboService.saveWeiBo();
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}
	
	
	/**
	 * 回复微博
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-3-14 下午10:30:47
	 */
	@RequestMapping(value = "/comment")
	public void comment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<TopicWeibo>> rds = new ResultDataStruct<List<TopicWeibo>>();
		String weiboStr;
		String topic;
		try {
			TopicWeibo weibo = null;
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}
	
	/**
	 * 转发微博
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-3-14 下午10:30:47
	 */
	@RequestMapping(value = "/forward")
	public void forward(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<TopicWeibo>> rds = new ResultDataStruct<List<TopicWeibo>>();
		String weiboStr;
		String topic;
		try {
			TopicWeibo weibo = null;
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}

	
	/**
	 * 根据话题Id查询微博列表
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author mazg
	 * @date 2012-3-14 下午10:30:47
	 */
	@RequestMapping(value = "/weiboComment")
	public void weiboComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<WeiboComment>> rds = new ResultDataStruct<List<WeiboComment>>();
		String weiboIdStr;
		try {
			weiboIdStr = request.getParameter("weiboId");
			int weiboId = Integer.parseInt(weiboIdStr);
			List<WeiboComment> commentList = weiboService.queryWeiboCommentByWeiboId(weiboId);
			rds.setData(commentList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}


	
	
	/**
	 * @param topicService
	 *            the topicService to set
	 */
	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

}
